package com.ruoyi.ufox.service.impl;


import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.ufox.domain.BisDeviceDataDay;
import com.ruoyi.ufox.domain.BisDeviceDataMonth;
import com.ruoyi.ufox.mapper.BisDeviceDataMonthMapper;
import com.ruoyi.ufox.service.IBisDeviceDataMonthService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 数据月统计Service业务层处理
 * 
 * @author ufoxs
 * @date 2022-08-22
 */
@Service
public class BisDeviceDataMonthServiceImpl implements IBisDeviceDataMonthService
{
    @Autowired
    private BisDeviceDataMonthMapper bisDeviceDataMonthMapper;

    /**
     * 查询数据月统计
     * 
     * @param recordId 数据月统计主键
     * @return 数据月统计
     */
    @Override
    public BisDeviceDataMonth selectBisDeviceDataMonthByRecordId(Long recordId)
    {
        return bisDeviceDataMonthMapper.selectBisDeviceDataMonthByRecordId(recordId);
    }

    /**
     * 查询数据月统计列表
     * 
     * @param bisDeviceDataMonth 数据月统计
     * @return 数据月统计
     */
    @Override
    public List<BisDeviceDataMonth> selectBisDeviceDataMonthList(BisDeviceDataMonth bisDeviceDataMonth)
    {
        return bisDeviceDataMonthMapper.selectBisDeviceDataMonthList(bisDeviceDataMonth);
    }

    /**
     * 新增数据月统计
     * 
     * @param bisDeviceDataMonth 数据月统计
     * @return 结果
     */
    @Override
    public int insertBisDeviceDataMonth(BisDeviceDataMonth bisDeviceDataMonth)
    {
        return bisDeviceDataMonthMapper.insertBisDeviceDataMonth(bisDeviceDataMonth);
    }

    /**
     * 修改数据月统计
     * 
     * @param bisDeviceDataMonth 数据月统计
     * @return 结果
     */
    @Override
    public int updateBisDeviceDataMonth(BisDeviceDataMonth bisDeviceDataMonth)
    {
        return bisDeviceDataMonthMapper.updateBisDeviceDataMonth(bisDeviceDataMonth);
    }

    /**
     * 批量删除数据月统计
     * 
     * @param recordIds 需要删除的数据月统计主键
     * @return 结果
     */
    @Override
    public int deleteBisDeviceDataMonthByRecordIds(Long[] recordIds)
    {
        return bisDeviceDataMonthMapper.deleteBisDeviceDataMonthByRecordIds(recordIds);
    }

    /**
     * 删除数据月统计信息
     * 
     * @param recordId 数据月统计主键
     * @return 结果
     */
    @Override
    public int deleteBisDeviceDataMonthByRecordId(Long recordId)
    {
        return bisDeviceDataMonthMapper.deleteBisDeviceDataMonthByRecordId(recordId);
    }

    @Override
    public List<BisDeviceDataMonth> getTimg(String deviceCode, String yearMonth) {
            List<BisDeviceDataMonth> list=this.bisDeviceDataMonthMapper.getTimg(deviceCode,yearMonth);

            //date   2022-08
            //date   2022-09

            //2022-08-01 +1个月 - 一天
        Date firstDate = DateUtils.parseDate(yearMonth+"-01");
        Date lastDate = DateUtils.addDays(DateUtils.addMonths(firstDate, 1), -1);




            for (int i = 1; i <=lastDate.getDate();i++){
                boolean aa=false;
                String currentDate = yearMonth+"-"+(StringUtils.leftPad(i+"", 2, '0'));
                for (BisDeviceDataMonth ltem: list) {
                    if (ltem.getYearMonth() != null && DateUtils.parseDateToStr("yyyy-MM-dd", ltem.getYearMonth()).equals(currentDate)) {
                        ltem.setDateTime(ltem.getDateTime().substring(8,10));
                        aa = true;
                        break;
                    }
                }
                if (aa==false) {
                    BisDeviceDataMonth dataDay=new BisDeviceDataMonth();
                    dataDay.setYearMonth(DateUtils.parseDate(currentDate));
                    dataDay.setDateTime(currentDate.substring(8,10));

                    dataDay.setTemerature(BigDecimal.valueOf(0));
                    dataDay.setHumidity(BigDecimal.valueOf(0));
                    dataDay.setLightIntensity(BigDecimal.valueOf(0));
                    list.add(i-1,dataDay);

                }
            }
            return list;
        }


    public static void main(String[] args) {

    String date = "2022-02";
        Date firstDate = DateUtils.parseDate(date+"-01");
        Date lastDate = DateUtils.addDays(DateUtils.addMonths(firstDate, 1), -1);

        System.out.println(lastDate.getDate());
    }
}
